package GUI; //done

import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Client.ConnectionToServer;
import Entities.Product;
import Entities.Template;

/**
 * This Class controls Product Search Window.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */

public class ProductSearchWindow extends JFrame{
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;

	/** The Product Name Label. */
	private JLabel pCNumlLabel = null;
	
	/** Search Button. */
	private JButton searchButton = null;
	
	/** The Product Name Label. */
	private JPanel jContentPane = null;
	
	/** The Product ID ComboBox. */
	private JComboBox pIDComboBox = null;
	
	/** The Product Window. */
	private ProductWindow productWindow = null;
	
	/** The Product. */
	private Product product = null;
	
	/** The Product Number. */
	private long prodNum = 0;
	
	/** The Product Name. */
	private String pname = null;
	
	/** The Product Window. */
	private String initialitoWindow;
	
	/** The Product Name Label. */
	private TemplateWindow templateform;
	
	/** The Product Name Label. */
	private Template template = null;
	
	/**
	 * The default constructor
	 */
	public ProductSearchWindow(String initialitoWindow){
		super();
		this.initialitoWindow = initialitoWindow;
		initialize();		
	}
	
	/**
	 * This function initializes ProductSearchWindow
	 */
	private void initialize(){
		product = new Product();
		this.setContentPane(getJContentPane());
		this.setTitle("Product Search");	
	}
	
	/**
	 * This function initializes JContentPane
	 */
	private JPanel getJContentPane(){
		if (jContentPane == null){
			jContentPane = new JPanel();
			jContentPane.setBackground(SystemColor.activeCaption);
			jContentPane.setLayout(null);
			
			pCNumlLabel = new JLabel();
			pCNumlLabel.setBounds(new Rectangle(58, 35, 113, 22));
			pCNumlLabel.setText("Select Product :");
							
			jContentPane.setLayout(null);
			jContentPane.add(pCNumlLabel, null);
			jContentPane.add(getpIDComboBox(), null);
			jContentPane.add(getSearchButton(),null);
			productWindow = new ProductWindow();
			productWindow.getcancelButton().addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setContentPane(jContentPane);								
				}
			});	
			
			templateform = new TemplateWindow();
			templateform.getBackjButton().addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setContentPane(jContentPane);						
				}
			});			
		}
		return jContentPane;		
	}
	
	/**
	 * This function initializes PIDComboBox	
	 */
	private JComboBox getpIDComboBox(){
		if(pIDComboBox == null){	
			pIDComboBox = new JComboBox();
			pIDComboBox.setBounds(new Rectangle(226, 34, 127, 25));
			String[] productname = null;
			try{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				productname = (String[]) connectiontoserver.loadAllProductsNames();
				if (productname == null){
					JOptionPane.showMessageDialog(null, "Catalogue is EMPTY!!!");
					pIDComboBox.addItem(" ");
				}	
				else{
					int len = productname.length;
					if (len > 0)
						pname = productname[0];
					if(len == 0)
						pIDComboBox.addItem("0");
					else
						pIDComboBox.addItem(" ");
					for(int i=0; i < len; i++){
						pIDComboBox.addItem(productname[i]);
					}	
				}
			}
			catch(Exception e){
				pIDComboBox.addItem(" ");
			}
			pIDComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					pname = (String)pIDComboBox.getSelectedItem();	
					if (pname.equals(":"))
						return;
					try{
						prodNum = Integer.parseInt(pname.substring(0, pname.indexOf(":")));
					}
					catch( NumberFormatException exc){
						return;
					}
				}	
			});	
		}	
		return pIDComboBox;
	}
	
	/**
	 * This function initializes searchButton	
	 */
	private JButton getSearchButton(){
		if (searchButton == null){
			searchButton = new JButton();
			searchButton.setBackground(SystemColor.control);
			searchButton.setBounds(new Rectangle(126, 104, 130, 29));
			searchButton.setText("Search");
			searchButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e){
					switch(initialitoWindow){
					case "UpdateProduct":
						initialiToUpdateProductForm();
						return;
						
					case "removeProduct":
						initialiToRemoveProductForm();
						return;
						
					case "CreateTemplate":
						initialiToCreateTemplateForm();
						return;
					}
				}	
			});
		}
		return searchButton;
	}
	
	/**
	 * This function initializes to UpdateProductForm
	 */
	private void initialiToUpdateProductForm(){
		try {
			if (prodNum > 0){
				if (product.loadProduct(prodNum)){
					productWindow.loadProduct(product);
					productWindow.addUpdateButton();
					productWindow.addShowPictureButton();
					this.setTitle("PRODUCT UPDATE");
					
					JLabel backgroundLabel = new JLabel("");
					backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
					backgroundLabel.setBounds(0, 0, 520, 400);
					productWindow.add(backgroundLabel);
					
					setContentPane(productWindow);
				}	
				else
					JOptionPane.showMessageDialog(null, "PRODUCT NOT EXIST IN DATA BASE");
			}
			else
				JOptionPane.showMessageDialog(null, "PLZ select a product");
		} 
		catch (Exception excp){
			JOptionPane.showMessageDialog(null, "ILEGAL INPUT");	
		}											
	}
	
	/**
	 * This function initializes to RemoveProductForm
	 */
	private void initialiToRemoveProductForm(){
		try{
			if (prodNum > 0){
				if (product.loadProduct(prodNum)){
					productWindow.loadProduct(product);
					productWindow.addRemoveButton();
					productWindow.addShowPictureButton();
					this.setTitle("PRODUCT REMOVE");
					
					JLabel backgroundLabel = new JLabel("");
					backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
					backgroundLabel.setBounds(0, 0, 520, 400);
					productWindow.add(backgroundLabel);
					
					setContentPane(productWindow);
				}	
				else
					JOptionPane.showMessageDialog(null, "PRODUCT NOT EXIST IN DATA BASE");
			}
			else
				JOptionPane.showMessageDialog(null, "PLZ select a product");
		} 
		catch (Exception excp){
			JOptionPane.showMessageDialog(null, "ERROR");	
		}											
	}
	
	/**
	 * This function initializes to CreateTemplateForm
	 */
	private void initialiToCreateTemplateForm(){
		try {
			if (prodNum > 0){
				if (product.loadProduct(prodNum)){
					template = new Template(0,"",product,"","");
					templateform.loadTemplate(template);
					templateform.addCreateButton();
					this.setTitle("Create template");
					
					JLabel backgroundLabel = new JLabel("");
					backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
					backgroundLabel.setBounds(0, 0, 520, 400);
					templateform.add(backgroundLabel);
					
					setContentPane(templateform);
				}	
				else
					JOptionPane.showMessageDialog(null, "PRODUCT NOT EXIST IN DATA BASE");
			}
			else
				JOptionPane.showMessageDialog(null, "PLZ select a product");
		} 
		catch (Exception excp){
			JOptionPane.showMessageDialog(null, "ERROR","",JOptionPane.ERROR_MESSAGE);	
		}											
	}
}
